<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String type = request.getParameter("type") == null ? "" : request.getParameter("type");
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String supplier_id = request.getParameter("supplier_id") == null ? "" : request.getParameter("supplier_id");
	String purchase_id = request.getParameter("purchase_id") == null ? "" : request.getParameter("purchase_id");
	boolean isAdd = "".equals(purchase_id);
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	String contact = String.valueOf(sessionMap.get("contact"));
	String email = String.valueOf(sessionMap.get("email"));
	String fax = String.valueOf(sessionMap.get("fax"));
%>
<html>
	<head>
		<title>补订货单</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var company_id;
		var company_name;
		$(function(){
			$('#company1_id').combogrid({
				title:'供货商选择',
				mode:"remote",
				loadMsg:'正在加载数据，请稍后......',
				panelWidth:550,
				panelHeight:400,
				url:"<%=contextPath %>/springMVC/crm/listPageSupplier",
				idField:"id",
				textField:"company",
				rownumbers:true,
				fitColumns:true,
				columns: [[
					{field:'id',hidden:true},
					{title:'公司名称',field:'company',width:200,align:'center'},
					{title:'联系人',field:'contact',width:200,align:'center'},
					{title:'TEL',field:'tel',width:200,align:'center'},
					{title:'FAX',field:'fax',width:200,align:'center'},
					{title:'MAIL',field:'mail',width:200,align:'center'}
				]],
				pageSize:10,
				pageList:[10,50,100],  
				pagination:true,
				onLoadSuccess:function(rtJson){
					jQuery('#company1_id').combogrid('setValue',company_id);
					jQuery('#company1_id').combogrid('setText',company_name);
				}
			});
			$('#company1_id').combogrid('disable');
			<%if(isAdd){%>
			var gd_code="";
			$("#operation_date").datetimebox('setValue',getTodaySecond());
				$.ajax({
					type:'GET',
					async:false,
					url:'<%=contextPath %>/springMVC/warehouse/getGDBaseById?gd_id=<%=gd_id%>',
					success:function(rtJson){
						$('#gd_admin_name').val(rtJson.gd_admin_name);	
						$('#gd_name').val(rtJson.gd_name);	
						$('#gd_code').val(rtJson.gd_code);	
						$('#company').val(rtJson.company2);
						gd_code=rtJson.gd_code;
					}
				});
				$.ajax({
					type:'GET',
					async:false,
					url:'<%=contextPath %>/springMVC/warehouse/getGdSupplierPurchase?gd_id=<%=gd_id%>',
					success:function(rtJson){
							if(rtJson.num>0){
								$('#code').val(gd_code+"-"+(parseInt(rtJson.num)+parseInt(1)));	
							}else{					
								$('#code').val(gd_code+"-"+1);	
							}
					}
				});
				$.ajax({
					type:'GET',
					url:'<%=contextPath %>/springMVC/crm/getSupplierById?id=<%=supplier_id%>',
					success:function(rtJson){
						company_id = rtJson.id;
						company_name = rtJson.company;
						$('#company1_id').combogrid('setValue',rtJson.id);
						$('#company1_id').combogrid('setText',rtJson.company);
						$('#admin1').val(rtJson.contact);	
						$('#tel1').val(rtJson.tel);	
						$('#fax1').val(rtJson.fax);	
						$('#mail1').val(rtJson.mail);	
					}
				});
			<%}else{%>
				$('#ff').form('load','<%=contextPath %>/springMVC/warehouse/getSupplierPurchaseById?purchase_id=<%=purchase_id%>');
				jQuery('#ff').form({
					onLoadSuccess:function(data){
						company_id = data.company1_id;
						company_name = data.company1;
					}
				});
			<%}%>
			$('#table').datagrid({
				<%=isAdd?"url:'"+contextPath+"/springMVC/gd/listGDSupplierApplyForPurchaseDetail?type="+type+"&gd_id="+gd_id+"&supplier_id="+supplier_id+"&yet=0',":"url:'"+contextPath+"/springMVC/warehouse/listSupplierPurchaseDetail?purchase_id="+purchase_id+"&yet=1',"%>
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
				    {title:'材料id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:150,align:'center'},
					{title:'产品名称',field:'name',width:300,align:'center'},
					{title:'规格/型号',field:'format_type',width:200,align:'center'},
					{title:'单位',field:'unit_name',width:100,align:'center'},
					{title:'单价',field:'cost',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							min:0,
							precision:2
						}
					}},
					{title:'订货数量',field:'num',width:100,align:'center'},
					{title:'合计',field:'total',width:100,align:'center',formatter: function(value,row,index){
						if(row.cost == null || row.num == null || row.cost == '' || row.num == ''){
							return "";
						}
							return Math.round(parseFloat(row.num)*parseFloat(row.cost)*Math.pow(10,2))/Math.pow(10,2);
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onAfterEdit:function(rowIndex, rowData, changes){
					setTotalMsg();
				},
				onLoadSuccess:function(rtJson){
					setTotalMsg();
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'cost'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						}
					}]
			});
			
		});
		
		function addDB(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
					var products="";
					var jsondata=$("#table").datagrid("getData");
					for(var i=0;i<jsondata.total;i++){
						if(products!=""){
							products+=";";
						}
						products+=jsonToStr(jsondata.rows[i]);
						var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
						if(jsondata.rows[i].cost == null || jsondata.rows[i].cost == "" || jsondata.rows[i].cost <= 0){
							$.messager.alert('提示','第'+index+'行价格填写不正确！');
							return ;
						}
					}
					
					var param = "code="+$('#code').val()+"&contract_code="+$('#contract_code').val()
								+"&company="+$('#company').val()+"&admin="+$('#admin').val()
								+"&tel="+$('#tel').val()+"&fax="+$('#fax').val()
								+"&mail="+$('#mail').val()+"&operation_date="+$('#operation_date').datetimebox('getValue')
								+"&company1_id="+$('#company1_id').combogrid('getValue')+"&company1="+$('#company1_id').combogrid('getText')
								+"&admin1="+$('#admin1').val()+"&remark="+$('#remark').val()
								+"&tel1="+$('#tel1').val()+"&fax1="+$('#fax1').val()
								+"&mail1="+$('#mail1').val()+"&total_money="+$('#total_money').html()
								+"&products="+products;
					$(obj).hide();
					$.ajax({
						type:'POST',
						url:'<%=contextPath%>/springMVC/warehouse/addSupplierPurchase?type=<%=type%>&gd_id=<%=gd_id%>&supplier_id=<%=supplier_id%>&purchase_id=<%=purchase_id%>',
						data:param,
						dataType:'json',
						success:function(rtJson){
							if(rtJson.rtState == '0'){
								eval('window.opener.<%=callback%>();');
								window.close();
							}
							else{
								$.messager.alert("提示",rtJson.rtMsrg);
							}
							$(obj).show();
						}
					});
			}
		}
		
	function setTotalMsg(){
		var total_num = 0;
		var total_money = 0;
		var jsondata=$("#table").datagrid("getData");
		for(var i=0;i<jsondata.total;i++){
			var row = jsondata.rows[i];
			if(row.cost == null || row.num == null || row.cost == '' || row.num == ''){
				continue;
			}
			total_num += parseFloat(row.num);
			total_money += Math.round(parseFloat(row.num)*parseFloat(row.cost)*Math.pow(10,2))/Math.pow(10,2);
		}
		$('#total_num').html(total_num);
		$('#total_money').html(total_money);
	}
	function gdPrint(){
		openDialogResize("<%=contextPath%>/springViews/print/startSupplierPurchasePrint.jsp?gd_id=<%=gd_id%>&supplier_id=<%=supplier_id%>&type=<%=type%>&purchase_id=<%=purchase_id%>",1000, 800);
	}
	</script>
	</head>
	<body>
		<div style="width: 90%;padding-left:50px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>订单编号：</td>
					<td class="td2" nowrap colspan='3'>
						<input  id="code" name="code" readonly>
					</td>
					<td align="right" class="td1" nowrap>合同编号：</td>
					<td class="td2" nowrap colspan='2'>
						<input  id="contract_code" name="contract_code" >
					</td>
				</tr>
				<tr>
					<td align="center" class="td1" nowrap rowspan='3'>甲<br>方</td>
					<td class="td2" nowrap colspan='6'>
						<input  id="company" name="company" size="100" readonly="readonly">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>商务管理员：</td>
					<td class="td2" nowrap >
						<input  id="admin" name="admin" value="<%=person%>"  readonly="readonly"/>
					</td>
					<td align="right" class="td1" nowrap>TEL：</td>
					<td class="td2" nowrap >
						<input  id="tel" name="tel"  value="<%=contact%>"/>
					</td>
					<td align="right" class="td1" nowrap>FAX：</td>
					<td class="td2" nowrap >
						<input  id="fax" name="fax" value="<%=fax%>"/ >
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>MAIL：</td>
					<td class="td2" nowrap colspan='3'>
						<input  id="mail" name="mail" size="50"  value="<%=email%>" />
					</td>
					<td align="right" class="td1" nowrap>订单日期：</td>
					<td class="td2" nowrap >
						<input  id="operation_date" name="operation_date" class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="center" class="td1" nowrap rowspan='3'>乙<br>方</td>
					<td class="td2" nowrap colspan='6'>
						<input  id="company1_id" name="company1_id" size="50" data-options="required:true" >
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>联系人：</td>
					<td class="td2" nowrap >
						<input  id="admin1" name="admin1" >
					</td>
					<td align="right" class="td1" nowrap>TEL：</td>
					<td class="td2" nowrap >
						<input  id="tel1" name="tel1" >
					</td>
					<td align="right" class="td1" nowrap>FAX：</td>
					<td class="td2" nowrap >
						<input  id="fax1" name="fax1" >
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>MAIL：</td>
					<td class="td2" nowrap colspan='5'>
						<input  id="mail1" name="mail1" size="50">
					</td>
				</tr>
				<tr>
					<td align="center" class="td1" nowrap rowspan='4'>订<br>货<br>单<br>信<br>息</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>工单管理员：</td>
					<td class="td2" nowrap colspan='5'>
						<input  id="gd_admin_name" name="gd_admin_name" size="50" readOnly>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap colspan='5'>
						<input  id="gd_name" name="gd_name" size="50" readOnly>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap colspan='5'>
						<input  id="gd_code" name="gd_code" size="50" readOnly>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='6'>
						<textarea  id="remark" name="remark" cols="100" ></textarea>
					</td>
				</tr>
						 <%if(!"1".equals(isShow)){ %>
				<tr>
					<td colspan="7"  nowrap>
						<div align="center">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addDB(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
							<%} else{%>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='6'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="gdPrint();">打印预览</a>
						</div>
					</td>
				</tr>
							<%}%>
				<tr>
					<td colspan="7"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
				<tr>
					<td colspan='7' nowrap>
						<div style="padding:3px" align="right">
							合计数量：<span id='total_num' style="color: red">0</span>
							合计金额：￥<span id='total_money' style="color: red">0</span>
						</div>
					</td>
				</tr>
			</table>
			</form>
		</div>
	</body>
</html>